<?php
/**
 * Class model that provides the base functionality to manage Attribute data
 *
 * This class was defined following the indications of
 * {@link http://www.overzealous.com/dmz/index.html DMZ ORM}. So,
 * this manage inserts, updates and deletes sql operations, as well as fully
 * set of instructions that can be used to make queries. And much more
 *
 * @author Gonzalez Federico, Viera Leonel
 * @package Protoweb
 * @subpackage model
 */
class Attribute extends PwModel 
{
    public $table      = 'attributes';
    public $model      = 'attribute';
    public $has_many   = array();
    public $has_one    = array('entity');
    public $validation = array(
        'name' => array(
            'label' => 'Name',
            'rules' => array(
                                'trim', 
                                'strtolower', 
                                'required', 
                                'unique_pair' => 'entity_id'
                            )
        ),
        'label' => array(
            'label' => 'Label',
            'rules' => array('trim', 'required')
        ),
        'rules' => array(
            'label' => 'Rules',
            'rules' => array('trim', 'strtolower')
        )
    );

    public function __construct($id = NULL)
    {
      parent::__construct($id);
    }

    public function firstSave($name, $label, $rules, $entityID, $purposeSlug)
    {
        $entity        = new Entity($entityID);
        $purpose       = new Purpose($purposeSlug);
        $this->name    = $name;
        $this->label   = $label;
        $this->rules   = $rules;
        $this->purpose = $purpose->slug;
        
        $this->save($entity);

        $column[$this->name] = array('type' => $purpose->dbDatatype);

        $CI =& get_instance();
        $CI->load->dbforge();
        $CI->dbforge->add_column($this->entity->getTableName(), $column);
    }

    public function saveAndRefactor($name, $label, $rules, $purposeSlug)
    {
        $purpose       = new Purpose($purposeSlug);
        $old           = $this->get_copy();
        $this->name    = $name;
        $this->label   = $label;
        $this->rules   = $rules;
        $this->purpose = $purpose->slug;

        $this->save();

        if (($old->name != $this->name) OR ($old->purpose != $purposeSlug)) {
            $column[$old->name] = array(
                'name' => $this->name,
                'type' => $purpose->dbDatatype
            );

            $CI =& get_instance();
            $CI->load->dbforge();
            $CI->dbforge->modify_column($this->entity->getTableName(), $column);
        }
    }

    public function delete()
    {
        $tableName  = $this->entity->getTableName();
        $columnName = $this->name;

        parent::delete();
        
        $CI =& get_instance();
        $CI->load->dbforge();
        $CI->dbforge->drop_column($tableName, $columnName);
    }

    public function getTemplate($template)
    {
        $data['entityControllerVar']    = '$data[\'' . $this->entity->singular . '\']';
        $data['attributeControllerVar'] = $data['entityControllerVar'] . '->' . $this->name;
        $data['attributeViewVar']       = '{$' . $this->entity->singular . '->' . $this->name . '}';
        $data['attribute']              = $this;

        $CI =& get_instance();
        $CI->smartyview->smarty->assign($data);
        $parsedPusposeFile = $CI->smartyview->smarty->fetch(Purpose::getFilePath($this->purpose));
        $parsedPusposeJSON = json_decode($parsedPusposeFile,true);

        return $parsedPusposeJSON['templates'][$template];
    }

} // Class Attribute

/* End of file attribute.php */
/* Location: ./application/models/attribute.php */
